package com.matrixlearning.controller;

import com.google.gson.Gson;
import com.matrixlearning.service.ComputeService;
import com.matrixlearning.utils.GsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/compute")
public class ComputeController {
    /**
     * Auther sun
     * DATE 2023/3/13 20:07
     * VERSION 1.0
     */

    private final static Logger logger = LoggerFactory.getLogger(ComputeController.class);

    @Autowired
    ComputeService computeService;

    /**
     * 矩阵加减
     * @param json
     * @return
     */
    @PostMapping("/addSub")
    public String addSub(@RequestBody String json) {
        Map map = GsonUtils.fromJson(json, Map.class);
        String token = (String)map.get("token");
        String input1 = (String)map.get("input1");
        String input2 = (String)map.get("input2");
        String input3 = (String)map.get("input3");
        String select = (String) map.get("select");
        String result = computeService.addSub(input1, input2, input3, select);
        return result;
    }

    @PostMapping("/characterValue")
    public String characterValue(@RequestBody String json) {
        Map map = GsonUtils.fromJson(json, Map.class);
        String token = (String)map.get("token");
        String input1 = (String)map.get("input1");
        String input2 = (String)map.get("input2");
        String[] result = computeService.characterValue(input1, input2);
        Map<String, Object> resp = new HashMap<>();
        resp.put("vector", result[0]);
        resp.put("value", result[1]);
        return GsonUtils.toJson(resp);
    }

    @PostMapping("/ladderMatrix")
    public String ladderMatrix(@RequestBody String json) {
        Map map = GsonUtils.fromJson(json, Map.class);
        String token = (String)map.get("token");
        String input1 = (String)map.get("input1");
        String input2 = (String)map.get("input2");
        int[][] result = computeService.ladderMatrix(input1, input2);
        StringBuilder builder = new StringBuilder();
        for(int i=0;i< result.length;i++) {
            for(int j= 0; j<result[0].length;j++) {
                builder.append(result[i][j]);
                builder.append(" ");
            }
        }
        return builder.toString();
    }
    @PostMapping("/mult")
    public String mult(@RequestBody String json) {
        Map map = GsonUtils.fromJson(json, Map.class);
        String token = (String)map.get("token");
        String input1 = (String)map.get("input1");
        String input2 = (String)map.get("input2");
        String input3 = (String)map.get("input3");
        String input4 = (String)map.get("input4");
        String[] result = computeService.mult(input1, input2, input3, input4);
        Map<String, Object> resp = new HashMap<>();
        resp.put("result", result[0]);
        resp.put("shape", result[1] + " " + result[2]);
        return GsonUtils.toJson(resp);
    }
    @PostMapping("/numMult")
    public String numMult(@RequestBody String json) {
        Map map = GsonUtils.fromJson(json, Map.class);
        String token = (String)map.get("token");
        String input1 = (String)map.get("input1");
        String input2 = (String)map.get("input2");
        String input3 = (String)map.get("input3");
        String result = computeService.numMult(input1, input2, input3);
        return result;
    }

    @PostMapping("/similiarDiag")
    public String similiarDiag(@RequestBody String json) {
        Map map = GsonUtils.fromJson(json, Map.class);
        String token = (String)map.get("token");
        String input1 = (String)map.get("input1");
        String input2 = (String)map.get("input2");
        String[] result = computeService.similiarDiag(input1, input2);
        Map<String, Object> resp = new HashMap<>();
        resp.put("result1", result[0]);
        resp.put("result2", result[1]);
        return GsonUtils.toJson(resp);
    }
}
